"""
Drop all tables and recreate the database schema.
"""
import sys
from pathlib import Path

# Add the backend directory to the path
sys.path.insert(0, str(Path(__file__).parent))

from sqlalchemy import text
from app.database import engine, Base
from app.models import user, room, booking, schedule, system_config

def main():
    print("=" * 60)
    print("Meeting Room Booking System - Database Reset")
    print("=" * 60)
    print()
    
    print("Dropping all tables...")
    try:
        with engine.connect() as conn:
            # Drop all tables
            conn.execute(text("DROP TABLE IF EXISTS bookings CASCADE"))
            conn.execute(text("DROP TABLE IF EXISTS schedules CASCADE"))
            conn.execute(text("DROP TABLE IF EXISTS rooms CASCADE"))
            conn.execute(text("DROP TABLE IF EXISTS users CASCADE"))
            conn.execute(text("DROP TABLE IF EXISTS system_configs CASCADE"))
            
            # Drop enum types
            conn.execute(text("DROP TYPE IF EXISTS userrole CASCADE"))
            conn.execute(text("DROP TYPE IF EXISTS roomstatus CASCADE"))
            conn.execute(text("DROP TYPE IF EXISTS bookingstatus CASCADE"))
            
            conn.commit()
        print("✓ All tables and enums dropped")
    except Exception as e:
        print(f"✗ Error dropping tables: {e}")
        return
    
    print()
    print("Creating new tables...")
    try:
        Base.metadata.create_all(bind=engine)
        print("✓ Tables created successfully")
    except Exception as e:
        print(f"✗ Error creating tables: {e}")
        return
    
    print()
    print("=" * 60)
    print("✅ Database reset complete!")
    print("=" * 60)
    print()
    print("Run 'python setup_db.py' to populate with initial data.")

if __name__ == "__main__":
    main()
